package com.controller;

import com.domain.*;
import com.service.CourseService;
import com.service.LoginService;
import com.service.StudentService;
import com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private LoginService loginService;
    
    /**
     * 课程模块：添加课程、编辑课程、查询课程、删除课程
     * @return
     */
    @RequestMapping("/toAddCourse")
    public String toAddCourse(HttpServletRequest request){
        List<Teacher> teachers = teacherService.findAllTeacher();
        List<College> colleges = teacherService.findAllCollege();
        request.setAttribute("teacherList",teachers);
        request.setAttribute("collegeList",colleges);
        return "admin/addCourse";
    }
    @RequestMapping("/toEditCourse")
    public String toEditCourse(String id,HttpServletRequest request){
        Course course = courseService.findCourseById(Integer.parseInt(id));
        List<Teacher> teachers = teacherService.findAllTeacher();
        List<College> colleges = teacherService.findAllCollege();
        request.setAttribute("teacherList",teachers);
        request.setAttribute("collegeList",colleges);
        request.setAttribute("course",course);
        return "admin/editCourse";
    }
    @RequestMapping("/addCourse")
    public String addCourse(Course course,HttpServletRequest request){
        int sum = courseService.totalCourse(1,0);
        course.setCourseID(sum+1);
        System.out.println(course);
        String result = courseService.addCourse(course);
        if (result.equals("null")){
            request.setAttribute("message","添加失败！请完善课程信息！");
            return "error";
        }
        int total = courseService.totalCourse(0,0);
        request.getSession().setAttribute("totalCourse",total);
        return "redirect:/admin/showCourse";
    }
    @RequestMapping("/editCourse")
    public String editCourse(Course course,HttpServletRequest request){
        courseService.updateCourse(course);
        return "redirect:/admin/showCourse";
    }
    @RequestMapping("/removeCourse")
    public String removeCourse(String id,HttpServletRequest request){
        courseService.deleteCourse(Integer.parseInt(id));
        int total = courseService.totalCourse(0,0);
        request.getSession().setAttribute("totalCourse",total);
        return "redirect:/admin/showCourse";
    }
    @RequestMapping("/showCourse")
    public String showCourse(String page,String name, HttpServletRequest request){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        PageBean bean = courseService.findAndSearchCourse(currentPage,name,user.getRole(),0);
        request.setAttribute("courseList",bean.getList());
        request.setAttribute("pageBean",bean);
        return "admin/showCourse";
    }


    /**
     * 学生模块：查询学生，添加学生，修改学生信息，删除学生
     */
    @RequestMapping("/showStudent")
    public String showStudent(String name,String page,HttpServletRequest request){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        PageBean bean = studentService.findAllStudent(name,currentPage);
        request.setAttribute("studentList",bean.getList());
        request.setAttribute("pageBean",bean);
        return "admin/showStudent";
    }
    @RequestMapping("/toAddStudent")
    public String toAddStudent(HttpServletRequest request){
        List<College> collegeList = teacherService.findAllCollege();
        request.setAttribute("collegeList",collegeList);
        return "admin/addStudent";
    }
    @RequestMapping("/addStudent")
    public String addStudent(Student student,HttpServletRequest request){
        System.out.println(student);
        Student stu = studentService.findById(String.valueOf(student.getUserID()));
        if (stu != null){
            request.setAttribute("message","学号"+student.getUserID()+"已存在！");
            return "error";
        }
        studentService.addStudent(student);
        int totalStudent = studentService.totalStudent(null);
        request.getSession().setAttribute("totalStudent",totalStudent);
        return "redirect:/admin/showStudent";
    }
    @RequestMapping("/toEditStudent")
    public String toEditStudent(String id,HttpServletRequest request){
        Student student = studentService.findById(id);
        List<College> collegeList = teacherService.findAllCollege();
        request.setAttribute("collegeList",collegeList);
        request.setAttribute("student",student);
        return "admin/editStudent";
    }
    @RequestMapping("/editStudent")
    public String editStudent(Student student,HttpServletRequest request){
        studentService.updateStudent(student);
        return "redirect:/admin/showStudent";
    }
    @RequestMapping("/removeStudent")
    public String removeStudent(String id,HttpServletRequest request){
        studentService.deleteStudent(Integer.parseInt(id));
        int totalStudent = studentService.totalStudent(null);
        request.getSession().setAttribute("totalStudent",totalStudent);
        return "redirect:/admin/showStudent";
    }


    /**
     * 教师模块：查询所有教师、删除教师信息、添加教师信息、修改教师信息
     */
    @RequestMapping("/showTeacher")
    public String showTeacher(String name,String page,HttpServletRequest request){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        PageBean bean = teacherService.findAndSearchTeacher(name,currentPage);
        request.setAttribute("teacherList",bean.getList());
        request.setAttribute("pageBean",bean);
        return "admin/showTeacher";
    }
    @RequestMapping("/toAddTeacher")
    public String toAddTeacher(HttpServletRequest request){
        List<College> collegeList = teacherService.findAllCollege();
        request.setAttribute("collegeList",collegeList);
        return "admin/addTeacher";
    }
    @RequestMapping("/addTeacher")
    public String addTeacher(Teacher teacher,HttpServletRequest request){
        System.out.println(teacher);
        teacherService.addTeacher(teacher);
        int totalTeacher = teacherService.totalTeacher(null);
        request.getSession().setAttribute("totalTeacher",totalTeacher);
        return "redirect:/admin/showTeacher";
    }
    @RequestMapping("/toEditTeacher")
    public String toEditTeacher(String id,HttpServletRequest request){
        Teacher teacher = teacherService.findTeacherById(Integer.parseInt(id));
        List<College> collegeList = teacherService.findAllCollege();
        request.setAttribute("collegeList",collegeList);
        request.setAttribute("teacher",teacher);
        return "admin/editTeacher";
    }
    @RequestMapping("/editTeacher")
    public String editTeacher(Teacher teacher){
        teacherService.updateTeacher(teacher);
        return "redirect:/admin/showTeacher";
    }
    @RequestMapping("/removeTeacher")
    public String removeTeacher(String id,HttpServletRequest request){
        teacherService.deleteTeacherById(Integer.parseInt(id));
        int totalTeacher = teacherService.totalTeacher(null);
        request.getSession().setAttribute("totalTeacher",totalTeacher);
        return "redirect:/admin/showTeacher";
    }


    /**
     * 重置密码
     */
    @RequestMapping("/toReset")
    public String toReset(){
        return "admin/userPasswordRest";
    }
    @RequestMapping("/userPasswordRest")
    public String userPasswordRest(String userName,String password,HttpServletRequest request){
            LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        if (!user.getPassword().equals(password)){
            request.setAttribute("message","管理员密码错误！");
            return "error";
        }
        loginService.resetPassword(userName);
        return "admin/userPasswordRest";
    }
}
